package com.ysd.lis.service.bac.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.bac.BacUnionColoCount;
import com.ysd.lis.entity.sta.StaColonyCount;
import com.ysd.lis.mapper.bac.BacUnionColoCountMapper;
import com.ysd.lis.service.bac.BacUnionColoCountService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cfz
 * @since 2024-04-07
 */
@Service
public class BacUnionColoCountServiceImpl extends ServiceImpl<BacUnionColoCountMapper, BacUnionColoCount> implements BacUnionColoCountService {

    @Autowired
    BacUnionColoCountMapper bacUnionColoCountMapper;

    @Override
    public Result findBacUnionColoCountList(BacUnionColoCount bacUnionColoCount) {
        List<BacUnionColoCount> list = findBacUnionColoCountArray(bacUnionColoCount);
        return Result.succ(1, "请求成功", list);
    }

    @Override
    public List<BacUnionColoCount> findBacUnionColoCountArray(BacUnionColoCount bacUnionColoCount) {
        MPJLambdaWrapper<BacUnionColoCount> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.eq(BacUnionColoCount::getDelFlag, 0);
        queryWrapper.eq(StringUtils.isNotBlank(bacUnionColoCount.getInputCode()), BacUnionColoCount::getInputCode, bacUnionColoCount.getInputCode());
        queryWrapper.eq(StringUtils.isNotBlank(bacUnionColoCount.getCode()), BacUnionColoCount::getCode, bacUnionColoCount.getCode());
        queryWrapper.eq(StringUtils.isNotBlank(bacUnionColoCount.getName()), BacUnionColoCount::getName, bacUnionColoCount.getName());
        if (StringUtils.isNotBlank(bacUnionColoCount.getFilter())) {
            queryWrapper.and(query -> query.like(BacUnionColoCount::getCode, bacUnionColoCount.getFilter()).or().like(BacUnionColoCount::getName, bacUnionColoCount.getFilter()));
        }
        queryWrapper.orderByAsc(BacUnionColoCount::getSeq);
        return bacUnionColoCountMapper.selectList(queryWrapper);
    }

    @Override
    public Result addBacUnionColoCount(BacUnionColoCount bacUnionColoCount) {
        int i = bacUnionColoCountMapper.insert(bacUnionColoCount);
        if (i > 0) {
            return Result.succ(1, "新增成功", bacUnionColoCount);
        }
        return Result.fail("新增失败");
    }

    @Override
    public Result deleteBacUnionColoCount(BacUnionColoCount bacUnionColoCount) {
        bacUnionColoCount.setDelFlag(1);
        int i = bacUnionColoCountMapper.updateById(bacUnionColoCount);
        if (i > 0) {
            return Result.succ(1, "删除成功", bacUnionColoCount);
        }
        return Result.fail("删除失败");
    }

    @Override
    public Result updateBacUnionColoCount(BacUnionColoCount bacUnionColoCount) {
        int i = bacUnionColoCountMapper.updateById(bacUnionColoCount);
        if (i > 0) {
            return Result.succ(1, "更新成功", bacUnionColoCount);
        }
        return Result.fail("更新失败");
    }

    @Override
    public Result staDataUnionColoCount(List<StaColonyCount> staColonyCounts) {
        for (StaColonyCount staColonyCount : staColonyCounts) {
            BacUnionColoCount bacUnionColoCount = new BacUnionColoCount();
            BeanUtils.copyProperties(staColonyCount, bacUnionColoCount, "id");
            bacUnionColoCount.setCode(staColonyCount.getColCountNo());
            bacUnionColoCount.setName(staColonyCount.getColCountNa());
            this.saveOrUpdate(bacUnionColoCount, new LambdaQueryWrapper<BacUnionColoCount>().eq(BacUnionColoCount::getCode, bacUnionColoCount.getCode()).eq(BacUnionColoCount::getDelFlag, 0));
        }
        return Result.succ(1, "同步成功！", null);
    }
}
